package com.example.controller;

import com.example.dao.IcommentDao;
import com.example.dao.ReplyDao;
import com.example.dao.UserInfoDao;
import com.example.model.pojo.Comment;
import com.example.model.pojo.Reply;
import com.example.model.pojo.Result;
import com.example.model.pojo.UserInfo;
import com.example.model.vo.CommentVo;
import com.example.service.CommentService;
import com.example.websocket.WebSocketProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 27954
 * @Description TODO
 * @Date 2022/9/20 19:49
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private IcommentDao icommentDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ReplyDao replyDao;
    @Autowired
    private WebSocketProcess webSocketProcess;
    @GetMapping("comments")
    public Result getComments(Integer discussId) {
        List<Comment> list = commentService.loadAllByComment(discussId);
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = userInfoDao.getByUuid(comment.getUuId());
            CommentVo cvo = new CommentVo();
            if (null != userInfo) {
                cvo.setId(comment.getCId());
                cvo.setUuId(comment.getUuId());
                cvo.setNickName(userInfo.getNickname());
                cvo.setRid(comment.getRId());
                cvo.setContent(comment.getContent());
                cvo.setCavatar(userInfo.getAvatar());
                cvo.setCountR(replyDao.countByToUid(comment.getUuId()));
            }
            commentVoList.add(cvo);
        }
        return new Result("200", "success", commentVoList);
    }

    @GetMapping("reply")
    public Result hello(Integer id, Integer cid, String msg, Integer touid,HttpServletRequest request)  {
        UserInfo userInfo= (UserInfo) request.getAttribute("userInfo");
        redisTemplate.opsForValue().set(String.valueOf(id),msg);
        redisTemplate.convertAndSend("chart",String.valueOf(id));
        Reply reply=new Reply();
        reply.setContent(msg);
        reply.setUid(userInfo.getUuid());
        reply.setCId(cid);
        reply.setToUid(touid);
        reply.setStatus(1);
        replyDao.save(reply);
        return new Result("200","success");
    }

    @GetMapping("rcomment")
    public Result rcomment(Integer cId){
        List<Reply> replyList=replyDao.findAllByCIdaAndToUid(cId);
        List<CommentVo> voList=new ArrayList<>();
        for (Reply reply : replyList) {
            UserInfo userInfo=userInfoDao.getByUuid(reply.getUid());
            UserInfo userInfo1=userInfoDao.getByUuid(reply.getToUid());
            CommentVo cc=new CommentVo();
            cc.setCavatar(userInfo.getAvatar());
            cc.setContent(reply.getContent());
            cc.setNickName(userInfo.getNickname());
            cc.setUuId(reply.getUid());
            cc.setRid(reply.getToUid());
            cc.setId(reply.getRid());
            cc.setRnickname(userInfo1.getNickname());
            voList.add(cc);
        }
        return new Result("200","success",voList);
    }

    @GetMapping("delcomment")
    public Result delComment(Integer cid, HttpServletRequest request){
        UserInfo userInfo= (UserInfo) request.getAttribute("userInfo");
        Integer uuid=userInfo.getUuid();
        commentService.delComment(cid,uuid);
        return new Result("200","success");
    }
    @GetMapping("delreply")
    public Result delReply(Integer rid, HttpServletRequest request){
        UserInfo userInfo= (UserInfo) request.getAttribute("userInfo");
        Integer uuid=userInfo.getUuid();
        replyDao.deleteByRidAndUid(rid,uuid);
        return new Result("200","success");
    }
}
